package com.stlm2.controller.informs;
import com.stlm2.dems.informs.entity.InformsState;
import com.stlm2.dems.informs.service.InformsService;

import com.stlm2.util.codecontent.CodeContent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by zlq on 2017/5/30.
 */
@Controller
@RequestMapping("/InformsC")
public class InformsController {
    @Resource
    private InformsService informsService;

    /*
*ZLQ
*查询新闻列表(后台)
 */
    @ResponseBody
    @RequestMapping(value="/getInformsList.json",produces="application/json;charset=UTF-8")
    public  String getInformsList(ModelMap modelMap , String page,String state){
        String data=null;
        try {
            if (StringUtils.isEmpty(state)) {
                return CodeContent.getModelByCode(0, "状态不能为空", modelMap);
            }
            InformsState InformsState1 = InformsState.getByIntValue(Integer.parseInt(state));
            data=informsService.getInformsList(modelMap,page, InformsState1);

        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询新闻列表失败",data);
        }
        return  data;
    }


    /*
*ZLQ
* 根据ID查询新闻详情(后台)
* Id：新闻ID
 */
    @ResponseBody
    @RequestMapping(value="/getDetails.json",produces="application/json;charset=UTF-8")
    public String getDetails (ModelMap model,String Id){
        String data=null;
        try {
            data=informsService.getDetails(model,Id);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询新闻详情失败",data);
        }
        return data;
    }
    /*
*ZLQ
*添加新闻信息(后台)
*title：标题
* content：内容
* token：用户token
*/
    @ResponseBody
    @RequestMapping(value="/addInforms.json",produces="application/json;charset=UTF-8")
    public String addInforms(ModelMap model,String title,String content ,String token ,String urlImg){
        String data=null;
        try {
            data=informsService.addInforms(model, title, content , token,urlImg);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"添加新闻失败",data);
        }
        return data;
    }


    /*
    *ZLQ
*修改新闻信息(后台)
* title：标题
* content：内容
* token：用户token
*
 */
    @ResponseBody
    @RequestMapping(value="/updateInforms.json",produces="application/json;charset=UTF-8")
    public String updateInforms(ModelMap model,String Id,String title,String content ,String token,String stateValue,String urlImg){
        String data=null;
        try {
            data=informsService.updateInforms(model,Id,title,content,token,stateValue,urlImg);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"修改新闻详情失败",data);
        }
        return data;
    }
}
